import { useEffect, useRef, useState } from "react"

function UseDjs(count = 5): [number, () => void, boolean] {
    const [num, setnum] = useState(count)
    const [disable, setdisable] = useState(false)
    const time = useRef<NodeJS.Timeout | null>(null)
    const fn = () => {
        setdisable(true)
        time.current = setInterval(() => {
            setnum(prev => prev - 1)
        }, 1000)
    }
    useEffect(() => {
        if (num === 0) {
            if (time.current) clearInterval(time.current)
            setdisable(false)
            setnum(count)
        }
    })
    return [num, fn, disable]
}

export default UseDjs
